<?php
/**
 * Created by PhpStorm.
 * User: yiyu
 * Email: smalleasyu@gmail.com
 * Date: 2020-01-18 23:10:53
 * Desc:
 */

namespace app\api\behavior;

class UAInit
{
    public function run(&$params)
    {
        if (isset($_SERVER['HTTP_UA'])) {
            $ua = $_SERVER['HTTP_UA'];
        } elseif (isset($_SERVER['HTTP_USER_AGENT'])) {
            $ua = $_SERVER['HTTP_USER_AGENT'];
        } else {
            $ua = '';
        }

        $match = explode('/', $ua);

        if (count($match) >= 7) {

            $str = substr($match[1], 0, strrpos($match[1], " "));
            define('DEVICE_TYPE', $str);

            $str = substr($match[2], 0, strrpos($match[2], " "));
            define('DEVICE_OS', $str);

            $str = substr($match[3], 0, strrpos($match[3], " "));
            define('DEVICE_BRANCH', $str);

            $str = substr($match[4], 0, strrpos($match[4], " "));
            define('CLIENT_VERSION', $str);

            $str = substr($match[5], 0, strrpos($match[5], " "));
            define('DEVICE_SOFT', $str);

            define('CLIENT_ID', $str);

            $str = substr($match[6], 0, strrpos($match[6], " "));
            $tmp = explode('X', $str);
            if (sizeof($tmp) >= 2) {
                define('DEVICE_WIDTH', $tmp[0]);
                define('DEVICE_HEIGHT', $tmp[1]);
            }

            if (strtolower(DEVICE_TYPE) == 'android') {
                $str = $match[7];
                define('DEVICE_CHANNEL', $str);
            } else {
                $str = substr($match[7], 0, strrpos($match[7], " "));
                define('DEVICE_CHANNEL', $str);
                if (isset($match[8])) {
                    define('DEVICE_SOURCE', $match[8]);//企业版
                }
            }

        } else {
            define('DEVICE_TYPE', '');
            define('DEVICE_OS', '');
            define('DEVICE_BRANCH', '');
            define('CLIENT_VERSION', '');
            define('DEVICE_SOFT', '');
            define('CLIENT_ID', '');
            define('DEVICE_WIDTH', '');
            define('DEVICE_HEIGHT', '');
            define('DEVICE_CHANNEL', '');
            define('DEVICE_SOURCE', '');
        }
    }
}